<!-- 商品列表 -->
<template>
	<view class="container">
		<!-- 筛选 -->
		<view class="filter-outside">
			<view class="filter-content">
				<view class="item" v-for="item in tabs" :key="item.value" :class="{'active': form.type === item.value}"
					@click="handleChangeTabs(item)">
					<text class="label">{{item.label}}</text>
					<view class="icon" v-if="item.showOrder">
						<text class="iconfont asc" :class="{'active': item.asc}">&#xe607;</text>
						<text class="iconfont desc" :class="{'active': item.desc}">&#xe607;</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 列表 -->
		<view class="list">
			<view class="list-item" v-for="(item, index) in list" :key="index" @click="handleNavToDetail(item)">
				<image class="pic" :src="item.pic" mode="aspectFill"></image>
				<view class="content">
					<view class="header">
						<text class="name">{{item.name}}</text>
						<button class="qrcode iconfont" @click.stop="handleShowQrcode()">&#xe60b;</button>
					</view>
					<view class="body">
						<view class="price-box">
							<text class="price">¥{{item.price}}</text>
							<text class="old-price">¥{{item.old_price}}</text>
						</view>
						<view class="coupon-group">
							<text class="coupon">领{{item.coupon}}元券</text>
						</view>
					</view>
					<view class="footer">
						<text class="label">月销量{{item.sales}}+ {{item.comment}}%好评</text>
						<button class="combo-btn btn-transition-scale9" @click.stop="handleChooseCombo()">选规格</button>
					</view>
				</view>
			</view>
		</view>

		<!-- 溯源二维码 -->
		<view class="qrcode-panel" v-if="qrcode.isShow">
			<view class="qrcode-content">
				<view class="qrcode">
					<image class="pic" :src="qrcode.url" mode="widthFix" show-menu-by-longpress></image>
					<text class="text">溯源码</text>
				</view>
				<button class="close-btn iconfont" @click="qrcode.isShow = false">&#xe60d;</button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { useShopStore } from '../store.js';

	const form = ref({
		page: 1,
		limit: 10,
		type: 'default'
	});
	const tabs = ref([
		{ label: '默认', value: 'default', showOrder: false },
		{ label: '价格', value: 'price', showOrder: true, asc: true, desc: false },
		{ label: '销量', value: 'sales', showOrder: false, },
	]);

	const handleChangeTabs = (item) => {
		if (form.value.type === item.value && !item.showOrder) return;

		if (form.value.type !== item.value && item.showOrder) {
			item.asc = true;
			item.desc = false;
		} else if (form.value.type === item.value && item.showOrder) {
			item.asc = !item.asc;
			item.desc = !item.desc;
		}

		form.value.type = item.value;
	}

	const list = [{
			name: '红蚂蚁黄蚂蚁黑蚂蚁一窝端',
			pic: '/pagesC-shopping/static/images/will-delete/05.png',
			price: '8.79',
			old_price: '9.80',
			coupon: '5',
			sales: '1000',
			comment: '95'
		},
		{
			name: '实蝇针蜂诱捕头诱捕器实蝇',
			pic: '/pagesC-shopping/static/images/will-delete/06.png',
			price: '8.79',
			old_price: '9.80',
			coupon: '5',
			sales: '1000',
			comment: '95'
		},
		{
			name: '果蝇引诱剂瓜果实蝇诱捕器',
			pic: '/pagesC-shopping/static/images/will-delete/07.png',
			price: '8.79',
			old_price: '9.80',
			coupon: '5',
			sales: '1000',
			comment: '95'
		},
	];

	// 跳转到商品详情
	const handleNavToDetail = (row) => {
		uni.navigateTo({
			url: '/pagesC-shopping/goods-detail/index'
		});
	}

	const qrcode = ref({
		isShow: false,
		url: '/pagesC-shopping/static/images/will-delete/qrcode.png',
	});

	// 显示溯源二维码
	const handleShowQrcode = (row) => {
		qrcode.value.isShow = true;
	}

	// 选择规格
	const handleChooseCombo = () => {
		useShopStore().SHOW_COMBO_PANEL();
	}
</script>

<style lang="scss" scoped>
	.container {
		margin-top: 10rpx;
	}

	.filter-outside {
		height: 80rpx;

		.filter-content {
			display: flex;
			align-items: center;
			height: inherit;
			padding-left: 7rpx;
			box-sizing: border-box;

			.item {
				display: flex;
				align-items: center;
				padding: 0 25rpx;

				.label {
					font-weight: 500;
					font-size: 28rpx;
					color: #222222;
				}

				.icon {
					display: flex;
					flex-direction: column;
					margin-left: 4rpx;

					.iconfont {
						font-size: 14rpx;
						color: #222222;

						&.asc {
							transform: rotate(-180deg) translateY(-2rpx);
						}

						&.desc {
							transform: translateY(-2rpx);
						}
					}
				}

				&.active {
					.label {
						color: #FE3333;
						font-weight: bold;
					}

					.icon {
						.iconfont.active {
							color: #FE3333;
						}
					}
				}
			}
		}
	}

	.list {
		padding: 0 25rpx 30rpx;

		&-item {
			display: flex;
			background: #FFFFFF;
			border-radius: 20px;
			padding: 24rpx 20rpx 30rpx 22rpx;
			box-sizing: border-box;

			&+.list-item {
				margin-top: 20rpx;
			}

			.pic {
				flex-shrink: 0;
				width: 200rpx;
				height: 200rpx;
				border-radius: 6rpx;
			}

			.content {
				flex-grow: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 20rpx;

				.header {
					display: flex;
					align-items: center;

					.name {
						flex-grow: 1;
						width: 0;
						font-weight: bold;
						font-size: 30rpx;
						color: #222222;
						line-height: 1.5;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.qrcode {
						flex-shrink: 0;
						font-size: 40rpx;
						color: #222222;
						background-color: transparent;
					}
				}

				.body {
					flex-grow: 1;

					.price-box {
						display: flex;
						align-items: center;

						.price {
							font-weight: bold;
							font-size: 32rpx;
							color: #FE3333;
							line-height: 1.8;
						}

						.old-price {
							font-weight: 500;
							font-size: 24rpx;
							color: #999999;
							text-decoration: line-through;
							margin-left: 13rpx;
						}
					}

					.coupon-group {
						display: flex;

						&+.coupon-group {
							margin-left: 10rpx;
						}

						.coupon {
							display: flex;
							align-items: center;
							height: 34rpx;
							font-weight: 500;
							font-size: 20rpx;
							color: #FE3333;
							line-height: 34px;
							background: #FFEFF4;
							border-radius: 8rpx;
							padding-right: 6rpx;
							overflow: hidden;

							&::before {
								content: '券';
								width: 30rpx;
								height: 34rpx;
								font-weight: 500;
								font-size: 20rpx;
								color: #FFFFFF;
								line-height: 34rpx;
								text-align: center;
								background: #FE3333;
								margin-right: 6rpx;
							}
						}
					}
				}

				.footer {
					display: flex;
					align-items: center;

					.label {
						flex-grow: 1;
						font-weight: 500;
						font-size: 24rpx;
						color: #999999;
					}

					.combo-btn {
						flex-shrink: 0;
						width: 126rpx;
						height: 48rpx;
						font-weight: 500;
						font-size: 26rpx;
						color: #FFFFFF;
						text-align: center;
						line-height: 48rpx;
						background: linear-gradient(239deg, #FF651E 0%, #FE3333 100%);
						border-radius: 25rpx;
					}
				}
			}
		}
	}

	.qrcode-panel {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 9999;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.6);

		.qrcode-content {
			text-align: center;

			.qrcode {
				padding: 20rpx;
				border-radius: 10rpx;
				background-color: white;

				.pic {
					display: block;
					width: 400rpx;
					height: 400rpx;
				}

				.text {
					display: block;
					font-size: 24rpx;
					color: #222222;
					text-align: center;
					margin-top: 20rpx;
				}
			}

			.close-btn {
				display: inline-block;
				font-size: 30rpx;
				color: white;
				border: 5rpx solid white;
				border-radius: 50%;
				padding: 10rpx;
				margin-top: 50rpx;
				background-color: transparent;
			}
		}
	}
</style>